﻿// ===============================================================================
// ParamNotNull.cs
// EOS Framework
// ===============================================================================
// Copyright (c) EOS Development Group. All rights reserved.
// ===============================================================================

namespace EOS.Framework.Tools.Aspects
{
    using System;
    using System.Diagnostics.CodeAnalysis;

    /// <summary>
    /// Aspect for verifying, that the value of a parameter with the specified name
    /// passed to the current method is not null. Can only assigned to methods.
    /// </summary>
    [Serializable]
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, AllowMultiple = true)]
    [SuppressMessage("Microsoft.Design", "CA1019")]
    public sealed class ParamNotNull : ParamValidationAspect
    {        
        /// <summary>
        /// Initializes a new instance of the <see cref="ParamNotNull"/> class
        /// with the name of the parameter to validate.
        /// </summary>
        /// <param name="name">The name of the property to valdiate. Cannot be null (Nothing in Visual Basic)
        /// or empty or have only blanks.</param>
        /// <exception cref="ArgumentNullException"><param name="paramName"/> is null (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentException"><paramref name="paramName"/> is a string of length 0
        /// or contains only blanks.</exception>
        public ParamNotNull(string name)
            : base(name)
        {
        }

        /// <summary>
        /// Called when when the value passed to the current method must be validated. The method secures 
        /// that the passed value is not null.
        /// Throws a <see cref="ArgumentNullException"/>, if the value is not valid.
        /// </summary>
        /// <param name="value">The value to valide.</param>
        /// <exception cref="ArgumentNullException"><paramref name="value"/> is null 
        /// (Nothing in Visual Basic).</exception>
        protected override void OnValidate(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(ParamName);
            }
        }
    }
}
